<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">

      <el-form-item label="爱洞ID" prop="platformId">
        <el-input v-model="queryParams.platformId" placeholder="请输入爱洞ID" clearable/>
      </el-form-item>
      <el-form-item label="消息内容" prop="content">
        <el-input v-model="queryParams.content" placeholder="请输入消息内容" clearable/>
      </el-form-item>
      <el-form-item label="发送类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择发送类型" clearable>
          <el-option v-for="dict in dict.type.sys_msg_type" :key="dict.value" :label="dict.label" :value="dict.value"/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:sysMsgLog:add']"
        >新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="msgList">

      <el-table-column label="发送类型" align="center" prop="type">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_msg_type" :value="scope.row.type"/>
        </template>
      </el-table-column>
      <el-table-column label="爱洞ID" align="center" prop="platformId"/>
      <el-table-column label="发送内容" align="center" prop="content"/>
      <el-table-column label="发送人" align="center" prop="createBy"/>
      <el-table-column label="发送时间" align="center" prop="createTime"/>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 推送消息 -->
    <el-dialog title="推送消息" :visible.sync="open" width="500px" append-to-body>

      <el-form :inline="true" :model="msgBo" class="demo-form-inline">

        <el-form-item label="发送类型" prop="type">
          <el-select v-model="msgBo.type" placeholder="请选择发送类型" clearable @change="showPlat">
            <el-option v-for="dict in dict.type.sys_msg_type" :key="dict.value" :label="dict.label" :value="dict.value"/>
          </el-select>
        </el-form-item>

        <el-form-item v-if="show" label="爱洞ID" prop="platformId" >
          <el-input type="number" v-model="msgBo.platformId" clearable>
          </el-input>
        </el-form-item>

        <el-form-item label="消息内容">
          <el-input type="textarea" v-model="msgBo.content" placeholder="消息内容"
                    style="width: 217px;"></el-input>
        </el-form-item>

      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="toSend">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>

    </el-dialog>

  </div>
</template>

<script>
import {
  list,add
} from "@/api/service/sys/sysMsgLog";

export default {
  name: "sysMsgLog",
  dicts: ['sys_msg_type'],
  data() {
    return {
      open: false,
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 封禁IP和设备记录表格数据
      msgList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        platformId: undefined,
        content: undefined,
        type: undefined
      },
      // 表单参数
      msgBo: {
        platformId: undefined,
        content: undefined,
        type: undefined
      },
      show: false
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询封禁IP和设备记录列表 */
    getList() {
      this.loading = true;
      list(this.queryParams).then(response => {
        this.msgList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    toSend() {
      add(this.msgBo).then(response => {
        this.getList();
        this.msgBo.platformId = undefined
        this.msgBo.type = undefined
        this.msgBo.content = undefined
        this.open = false
        this.show = false
      });
    },
    cancel() {
        this.msgBo.platformId = undefined
        this.msgBo.type = undefined
        this.msgBo.content = undefined
        this.open = false
        this.show = false
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    handleAdd(){
      this.open = true
    },
    showPlat(value){
      this.show = parseInt(value) === 6;
    }
  }
};
</script>
